package com.kaifamiao.month04.day06;

public class MyLinkedListDouble {

    private Node first;
    private Node last;
    private int count;

    // 添加元素
    public void addFirst(Object obj) {
        Node node = new Node(null, obj, first);
        if (first == null) {
            last = node;
        } else {
            first.setPrev(node);
        }
        first = node;
        count++;
    }

    public void addLast(Object obj) {
        Node node = new Node(last, obj, null);
        if (last == null) {
            first = node;
        } else {
            last.setNext(node);
        }
        last = node;
        count++;
    }

    public void add(Object obj) {
        addLast(obj);
    }

    public void add(int index, Object obj) {
        if (checkIndex(index)) {
            if (index == 1) {
                addFirst(obj);
            } else if (index == count + 1) {
                addLast(obj);
            } else {
                Node prev = getNode(index - 1);
                Node next = prev.getNext();
                Node current = new Node(prev, obj, next);
                prev.setNext(current);
                next.setPrev(current);
                count++;
            }
        }
    }

    public Object getFirst() {
        if (first == null) {
            return null;
        } else {
            return first.getValue();
        }
    }

    public Object getLast() {
        if (last == null) {
            return null;
        } else {
            return last.getValue();
        }
    }

    public Object get(int index) {
        if (checkIndex(index)) {
            if (index == 1) {
                return getFirst();
            } else if (index == count) {
                return getLast();
            } else {
                return getNode(index).getValue();
            }
        } else {
            System.err.println("输入合理的位置！！");
        }
        return null;
    }

    private Node getNode(int index) {
        Node node = first;
        for (int i = 1; i < index; i++) {
            node = node.getNext();
        }
        return node;
    }

    private boolean checkIndex(int index) {
        return index <= count + 1 && index > 0;
    }

    public Object removeFirst() {
        if (first == null) {
            return null;
        } else {
            Node next = first.getNext();
            next.setPrev(null);
            first.setNext(null);
            count--;
            return first.getValue();
        }
    }

    public Object removeLast() {
        if (last == null) {
            return null;
        } else {
            Node prev = getNode(count - 1);
            Node l = last;
            last = prev;
            prev.setNext(null);
            l.setPrev(null);
            count--;
            return l.getValue();
        }
    }

    public Object remove(int index) {
        if (checkIndex(index)) {
            if (index == 1) {
                return removeFirst();
            } else if (index == count) {
                return removeLast();
            } else {
                Node node = getNode(index);
                Node prev = node.getPrev();
                prev.setNext(null);
                node.setPrev(null);
                count--;
                return node.getValue();
            }
        } else {
            System.out.println("输入正确的位置！！");
            return null;
        }
    }

    public int size() {
        return count;
    }
}
